<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>105-集合</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#集合">集合</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#基本操作">基本操作</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#关系测试">关系测试</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#常用集合方法">常用集合方法</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#add,-update">add, update</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#set.pop()">set.pop()</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#set.remove(obj)">set.remove(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#set.discard(obj)">set.discard(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#set.clear()">set.clear()</a>
                        </li>
                    
                
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 打印所有单行变量</span>
<span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#38598;&#21512;">&#38598;&#21512;<a class="anchor-link" href="#&#38598;&#21512;">&#182;</a></h2><p>是一个<code>无序</code> <code>不重复</code>可变元素集(但元素必须是不可变的元素)，主要用作去重和关系测试，和字典一样使用<code>{}</code>，所以创建空集合不用{}，用set()</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#22522;&#26412;&#25805;&#20316;">&#22522;&#26412;&#25805;&#20316;<a class="anchor-link" href="#&#22522;&#26412;&#25805;&#20316;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;aabb&#39;</span><span class="p">)</span>
<span class="n">x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[2]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;, &#39;b&#39;}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果创建集合时有可变元素则会报错</p>

<pre><code>x = {1,[1, 2]}
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: unhashable type: 'list'</code></pre>
<p>unhashable --&gt; 不可哈希 --&gt; 可变<br>
hashable   --&gt; 可哈希   --&gt; 不可变</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#20851;&#31995;&#27979;&#35797;">&#20851;&#31995;&#27979;&#35797;<a class="anchor-link" href="#&#20851;&#31995;&#27979;&#35797;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">}</span>

<span class="n">x</span> <span class="o">|</span> <span class="n">y</span>  <span class="c1"># --&gt; 并集</span>
<span class="n">x</span> <span class="o">&amp;</span> <span class="n">y</span>  <span class="c1"># --&gt; 交集</span>
<span class="n">x</span> <span class="o">-</span> <span class="n">y</span>  <span class="c1"># --&gt; 差集</span>
<span class="n">x</span> <span class="o">^</span> <span class="n">y</span>  <span class="c1"># --&gt; 对称差集</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[12]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1, 2, 3, 4}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[12]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{2, 3}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[12]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[12]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1, 4}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#24120;&#29992;&#38598;&#21512;&#26041;&#27861;">&#24120;&#29992;&#38598;&#21512;&#26041;&#27861;<a class="anchor-link" href="#&#24120;&#29992;&#38598;&#21512;&#26041;&#27861;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="nb">set</span><span class="p">())</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)]</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[8]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;add&#39;,
 &#39;clear&#39;,
 &#39;copy&#39;,
 &#39;difference&#39;,
 &#39;difference_update&#39;,
 &#39;discard&#39;,
 &#39;intersection&#39;,
 &#39;intersection_update&#39;,
 &#39;isdisjoint&#39;,
 &#39;issubset&#39;,
 &#39;issuperset&#39;,
 &#39;pop&#39;,
 &#39;remove&#39;,
 &#39;symmetric_difference&#39;,
 &#39;symmetric_difference_update&#39;,
 &#39;union&#39;,
 &#39;update&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="add,-update">add, update<a class="anchor-link" href="#add,-update">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">x</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
<span class="n">x</span>  <span class="c1"># --&gt;</span>

<span class="n">x</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>  <span class="c1"># 拆分更新</span>
<span class="n">x</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[20]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;abc&#39;, 1, 2, 3}</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[20]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;abc&#39;, 1, 2, 3, &#39;c&#39;, &#39;a&#39;, &#39;b&#39;}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="set.pop()">set.pop()<a class="anchor-link" href="#set.pop()">&#182;</a></h4><p>随机删除元素并返回该元素，不存在则报错</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">x</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[15]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="set.remove(obj)">set.remove(obj)<a class="anchor-link" href="#set.remove(obj)">&#182;</a></h4><p>删除指定元素，若集合中没有该元素则报错，修改原集合<code>没有</code>返回值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">x</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="n">x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[14]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{2, 3}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="set.discard(obj)">set.discard(obj)<a class="anchor-link" href="#set.discard(obj)">&#182;</a></h4><p>删除指定元素，集合中没有该元素不报错，修改原集合<code>没有</code>返回值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">x</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">x</span>             <span class="c1"># --&gt;</span>
<span class="n">x</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>  <span class="c1"># do nothing</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[21]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{2, 3}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="set.clear()">set.clear()<a class="anchor-link" href="#set.clear()">&#182;</a></h4><p>清空集合</p>

</div>
</div>
</div>
    </div>
  </div>
</body>

 


</html>
